package com.contract.commonlib.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.CheckBox;
import android.widget.TextView;

import com.contract.commonlib.R;
import com.google.android.material.internal.FlowLayout;

import java.util.ArrayList;
import java.util.List;


/**
 * 复选框控制按钮
 *
 * @author Administrator
 *
 */
@SuppressLint("RestrictedApi")
public class CheckBoxGroup extends FlowLayout {

	/**
	 * 选择控件
	 */
	private List<CheckBox> checkBoxs = new ArrayList<CheckBox>();

	/**
	 * 是否单选
	 */
	private boolean isRadio = false;

	Context context;
	public CheckBoxGroup(Context context) {

		super(context);
	}

	TypedArray ta;

	public CheckBoxGroup(Context context, AttributeSet attrs) {

		super(context, attrs);
		this.context = context;
		ta = context.obtainStyledAttributes(attrs, R.styleable.CheckBoxGroup);
		isRadio = ta.getBoolean(R.styleable.CheckBoxGroup_isRadio, false);
		ta.recycle();
	}

	public CheckBoxGroup(Context context, AttributeSet attrs, int defStyle) {

		super(context, attrs, defStyle);
	}

	private void showCheckbox() {
		for (int i = 0; i < checkBoxs.size(); i++) {
			CheckBox cbx = checkBoxs.get(i);
			final TextView tv = new TextView(context);
			tv.setTag(i);
			cbx.setOnCheckedChangeListener((buttonView, isChecked) -> {
				// 如果是单选,则也可以点击取消。
                if (buttonView.isPressed()) {
                    if (isRadio && isChecked) {
                        // 单选，并且选中状态为true，把其他选中状态取消。
                        unAllChecked();
                    }
                    buttonView.setChecked(isChecked);
                    if (changeListener!=null) {
                        changeListener.onChangeClick((int)tv.getTag());
                    }
                }

			});
			addView(cbx);
		}
	}

	/**
	 * 全部取消选中
	 */
	public void unAllChecked() {
		for (int i = 0; i < checkBoxs.size(); i++) {
			checkBoxs.get(i).setChecked(false);
		}
	}

	/**
	 * 设置第一个为选中状态,其余的不选中
	 */
	public void setCheckedByFirst(){
		for (int i = 0; i < checkBoxs.size(); i++) {
			checkBoxs.get(i).setChecked(i == 0);
		}
	}

	/**
	 * 取消选中
	 */
	public void cancelCheckd(int position){
		for (int i = 0; i < checkBoxs.size(); i++) {
			if (i==position) {
				checkBoxs.get(i).setChecked(false);
			}
		}
	}

	/**
	 * 获取选中的checkBox
	 */
	public List<CheckBox> getChecked() {
		List<CheckBox> list = new ArrayList<CheckBox>();
		for (int i = 0; i < checkBoxs.size(); i++) {
			CheckBox checkBox = checkBoxs.get(i);
			if (checkBox.isChecked()) {
				list.add(checkBox);
			}
		}
		return list;
	}

	public void setCheckBoxs(List<CheckBox> checkBoxs) {
		this.checkBoxs = checkBoxs;
		showCheckbox();
	}

	public boolean isRadio() {

		return isRadio;
	}

	public void setRadio(boolean isRadio) {

		this.isRadio = isRadio;
	}
	OnCheckedHasChangeListener changeListener;
	public void registerChangeListener(OnCheckedHasChangeListener changeListener){
		this.changeListener = changeListener;
	}
	public interface OnCheckedHasChangeListener{
		void onChangeClick(int position);
	}
}
